package com.java.app.hacktouch.mouse

import android.app.AlertDialog
import android.content.Context
import android.widget.Button
import android.widget.SeekBar
import android.widget.TextView
import com.java.app.hacktouch.R
import com.java.app.hacktouch.parameter.ParamsDB
import com.java.app.hacktouch.view.HackView

/**
 * Created by guanguojin on 2017/11/7.
 */

object MouseDialog {

    private var x_threshold = 0
    private var y_threshold = 0
    private var cursor_setup = 0
    private var circle_size = 0

    fun promptDialog(mContext: Context,
                     mouseDialog: AlertDialog,
                     params: ParamsDB,
                     hackview: HackView) {

        x_threshold = params.GetMouseParams()!!.xOffset
        y_threshold = params.GetMouseParams()!!.yOffset
        cursor_setup = params.GetMouseParams()!!.cursorStep
        circle_size = params.GetMouseParams()!!.circleSize

        var tv = mouseDialog.window!!.findViewById<TextView>(R.id.tv_x_threshold)
        tv.text = (mContext.resources.getString(R.string.mouse_x_threshold)
                + " : "
                + x_threshold.toString())
        tv = mouseDialog.window!!.findViewById(R.id.tv_y_threshold)
        tv.text = (mContext.resources.getString(R.string.mouse_y_threshold)
                + " : "
                + y_threshold.toString())
        tv = mouseDialog.window!!.findViewById(R.id.tv_cursor_setup)
        tv.text = (mContext.resources.getString(R.string.mouse_cursor_setup)
                + " : "
                + cursor_setup.toString())
        tv = mouseDialog.window!!.findViewById(R.id.tv_circle_size)
        tv.text = (mContext.resources.getString(R.string.mouse_circle_size)
                + " : "
                + circle_size.toString())

        val sb_x_threshold = mouseDialog.window!!.findViewById<SeekBar>(R.id.seekbar_x_threshold)
        sb_x_threshold.progress = params.GetMouseParams()!!.getxOffset()

        sb_x_threshold.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, i: Int, b: Boolean) {
                val tv = mouseDialog.window!!.findViewById<TextView>(R.id.tv_x_threshold)
                tv.text = (mContext.resources.getString(R.string.mouse_x_threshold)
                        + " : "
                        + seekBar.progress.toString())
                x_threshold = seekBar.progress
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {

            }

            override fun onStopTrackingTouch(seekBar: SeekBar) {

            }
        })

        val sb_y_threshold = mouseDialog.window!!.findViewById<SeekBar>(R.id.seekbar_y_threshold)
        sb_y_threshold.progress = params.GetMouseParams()!!.getyOffset()
        sb_y_threshold.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, i: Int, b: Boolean) {
                val tv = mouseDialog.window!!.findViewById<TextView>(R.id.tv_y_threshold)
                tv.text = (mContext.resources.getString(R.string.mouse_y_threshold)
                        + " : "
                        + seekBar.progress.toString())
                y_threshold = seekBar.progress
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {

            }

            override fun onStopTrackingTouch(seekBar: SeekBar) {

            }
        })

        val sb_cursor_setup = mouseDialog.window!!.findViewById<SeekBar>(R.id.seekbar_cursor_setup)
        sb_cursor_setup.progress = params.GetMouseParams()!!.cursorStep
        sb_cursor_setup.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, i: Int, b: Boolean) {
                val tv = mouseDialog.window!!.findViewById<TextView>(R.id.tv_cursor_setup)
                tv.text = (mContext.resources.getString(R.string.mouse_cursor_setup)
                        + " : "
                        + seekBar.progress.toString())
                cursor_setup = seekBar.progress
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {

            }

            override fun onStopTrackingTouch(seekBar: SeekBar) {

            }
        })

        val sb_circle_size = mouseDialog.window!!.findViewById<SeekBar>(R.id.seekbar_circle_size)
        sb_circle_size.progress = params.GetMouseParams()!!.circleSize
        sb_circle_size.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, i: Int, b: Boolean) {
                val tv = mouseDialog.window!!.findViewById<TextView>(R.id.tv_circle_size)
                tv.text = (mContext.resources.getString(R.string.mouse_circle_size)
                        + " : "
                        + seekBar.progress.toString())
                circle_size = seekBar.progress

            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {

            }

            override fun onStopTrackingTouch(seekBar: SeekBar) {

            }
        })

        val bt_confirm = mouseDialog.window!!.findViewById<Button>(R.id.bt_confirm)
        bt_confirm.setOnClickListener {
            val newParams = params.GetMouseParams()
            newParams!!.xOffset = x_threshold
            newParams.yOffset = y_threshold
            newParams.cursorStep = cursor_setup
            newParams.circleSize = circle_size
            params.SetMouseParams(newParams)
            hackview.setMouseParams(newParams)
            mouseDialog.dismiss()
        }

        val bt_cancel = mouseDialog.window!!.findViewById<Button>(R.id.bt_cancel)
        bt_cancel.setOnClickListener { mouseDialog.dismiss() }

    }
}
